﻿using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Tools;

namespace LapJointPoint
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "")
            {
                //获取文本框的值
                string[] A = textBox1.Text.Split(',', '，');
                string[] B = textBox2.Text.Split(',', '，');
                double ACNorm = double.Parse(textBox3.Text.ToString());
                double L = double.Parse(textBox4.Text.ToString());
                double d = double.Parse(textBox5.Text.ToString());
                //判断坐标格式是否正确
                if (A.Length != 2 || B.Length != 2)
                {
                    MessageBox.Show("坐标格式错误，请重新输入坐标！！");
                    return;
                }
                ArrayList list = new ArrayList();
                for (int i = 0; i < A.Length; i++)
                {
                    list.Add(A[i]);
                    list.Add(B[i]);
                }
                foreach (string item in list)
                {
                    if (RegularExpression(item) != true)
                    {
                        MessageBox.Show("坐标格式错误，请重新输入坐标！！");
                        return;
                    }
                }
                //AB向量
                Point pointA = new Point(double.Parse(A[0]), double.Parse(A[1]));
                Point pointB = new Point(double.Parse(B[0]), double.Parse(B[1]));
                Vector vectorAB = new Vector(pointB.X - pointA.X, pointB.Y - pointA.Y);
                //利用AB的单位向量计算点C的坐标
                Point pointC = new Point();
                pointC.X = System.Math.Round(pointA.X + ACNorm * (vectorAB.X / Vector.Norm(vectorAB.X, vectorAB.Y)), 2);
                pointC.Y = System.Math.Round(pointA.Y + ACNorm * (vectorAB.Y / Vector.Norm(vectorAB.X, vectorAB.Y)), 2);
                //AC，CB向量
                Vector vectorAC = new Vector(pointC.X - pointA.X, pointC.Y - pointA.Y);
                Vector vectorCB = new Vector(pointB.X - pointC.X, pointB.Y - pointC.Y);
                //利用AC的单位向量和长度计算点D的坐标
                Point pointD = new Point();
                pointD.X = System.Math.Round(pointC.X - (L / 2) * (vectorAC.X / Vector.Norm(vectorAC.X, vectorAC.Y)), 2);
                pointD.Y = System.Math.Round(pointC.Y - (L / 2) * (vectorAC.Y / Vector.Norm(vectorAC.X, vectorAC.Y)), 2);
                textBox6.Text = pointD.X + "," + pointD.Y;
                //利用CB的单位向量和长度计算点F的坐标
                Point pointF = new Point();
                pointF.X = System.Math.Round(pointC.X + (L / 2) * (vectorCB.X / Vector.Norm(vectorCB.X, vectorCB.Y)), 2);
                pointF.Y = System.Math.Round(pointC.Y + (L / 2) * (vectorCB.Y / Vector.Norm(vectorCB.X, vectorCB.Y)), 2);
                textBox8.Text = pointF.X + "," + pointF.Y;
                //用两条直角边（长度和半径）求出tan角度
                double Angle = System.Math.Atan(d / L) * 180;
                //带入旋转角函数 计算点E，点G坐标
                Point pointE = Tools.Math.Rotation(pointD, pointB, L, Angle);
                Point pointG = Tools.Math.Rotation(pointA, pointF, L, -Angle);
                textBox7.Text = pointE.X + "," + pointE.Y;
                textBox9.Text = pointG.X + "," + pointG.Y;
            }
            else
            {
                MessageBox.Show("数值不可以为空!错误");
                return;
            }
        }
        //判断文本框出入的格式是否正确；
        bool RegularExpression(string y)
        {
            bool Expression = Regex.IsMatch(y, @"(^-?[0-9]{1,}$)|(^-?[0-9]{1,}\.{1}[0-9]{1,}$)");
            return Expression;
        }
        //限制文本框只能输入数字
        private void LimitNumbers(KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 45)
            {
                e.Handled = true;
                MessageBox.Show("请输入有效数值！！");
            }
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            LimitNumbers(e);
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            LimitNumbers(e);
        }

        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {
            LimitNumbers(e);
        }
    }
}
